﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Black_Jack_Basic_Strategy
{
    class Deck
    {

        private int m_NumberOfDecks;
        private List<Card> m_Cards;
        private int m_CurrentCardIndex = 0;

        public Deck(int NumberOfDecks)
        {
            this.m_NumberOfDecks = NumberOfDecks;
            //Initialize the cards array to unshuffled:
            int Suits = 4;
            int CardsPerSuit = 13;
            int Count = 0;
            int ArraySize = Suits * CardsPerSuit * NumberOfDecks;
            List<Card> cards = new List<Card>();
            for (int i = 0; i < NumberOfDecks; i++)
            {
                for (int j = 0; j < Suits; j++)
                {
                    for (int k = 0; k < CardsPerSuit; k++)
                    { 
                        Card c = new Card();
                        c.Suit = (Card.CardSuits)j;
                        c.Value = (Card.CardValues)k;
                        cards.Add(c);
                        Count++;
                    }
                }
            }
            m_Cards = cards;
        }

        public bool NeedsShuffle(int MinCardsInDeck)
        {
            if (m_CurrentCardIndex + MinCardsInDeck > this.m_Cards.Count)
                return true;
            else
                return false;
        }

        public void Shuffle()
        {
            m_Cards = MixList(m_Cards);
            m_CurrentCardIndex = 0;
        }


        private List<E> MixList<E>(List<E> inputList)
        {
            List<E> randomList = new List<E>();

            Random r = new Random();
            int randomIndex = 0;
            while (inputList.Count > 0)
            {
                randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
                randomList.Add(inputList[randomIndex]); //add it to the new, random list
                inputList.RemoveAt(randomIndex); //remove to avoid duplicates
            }

            return randomList; //return the new random list
        }

        public Card GetNextCard()
        {
            Card c = m_Cards[m_CurrentCardIndex];
            m_CurrentCardIndex++;
            return c;
        }
    
    }
}
